package com.reggie.service.impl;

import com.reggie.constant.StatusConstant;
import com.reggie.entity.Orders;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.OrderMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.mapper.UserMapper;
import com.reggie.service.WorkSpaceService;
import com.reggie.vo.BusinessDataVO;
import com.reggie.vo.DishOverViewVO;
import com.reggie.vo.OrderOverViewVO;
import com.reggie.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询指定时间段内的营业数据（营业额、有效订单、订单完成率、平均客单价、新增用户数）
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin ,LocalDateTime end) {
        //营业额 select sum(amount) from orders where order_time > ? and order_time < ? and status = 5
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //查询订单总数
        Integer totalOrderCount = orderMapper.countByMap(map);
        //设置订单状态 重新封装到map进行判断
        map.put("status",Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        //如果当天没有营业额就赋值为0 有的话就返回当天的营业额
        turnover = turnover == null?0.0:turnover;

        //有效订单
        Integer validOrderCount = orderMapper.countByMap(map);
        Double uniPrice = 0.0;
        Double orderCompletionRate = 0.0;  //平均订单价
        if (totalOrderCount != 0&& validOrderCount !=0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue()/totalOrderCount;

            //平均客单价
            uniPrice = turnover/validOrderCount;
        }
        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return  BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(uniPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();
        map.put("beginTime", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        //查询启用的菜品数量
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        //查询禁用的菜品数量
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        //启用的套餐数量
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        //禁用的套餐数量
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
